package cn.bocweb.spinning.view.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ImageView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.RegisterDocBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/3/4.
 */
public class RegisterNoticeFragment extends DialogFragment {
    @Bind(R.id.register_notice_webview)
    WebView registerNoticeWebview;
    @Bind(R.id.register_notice_exit)
    ImageView registerNoticeExit;

    UserPresenter presenter;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtil.dip2px(getActivity(), 300));
        View view = inflater.inflate(R.layout.fragment_register_notice, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        presenter = new UserPresenterImp();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        presenter.registerDoc();

        registerNoticeExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterNoticeFragment.this.dismiss();
            }
        });
    }

    public void onEventMainThread(RegisterDocBean bean) {
        if (bean.getReturn_code() == 0) {
            registerNoticeWebview.loadUrl(bean.getUrl());
        }
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
